{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 直接使用 Pandas 加载数据\n",
    "df = pd.read_csv(\"data.csv\")\n",
    "texts = df['text'].tolist()\n",
    "labels = df['label'].tolist()\n",
    "\n",
    "# 简单的模型训练循环\n",
    "for text, label in zip(texts, labels):\n",
    "    # 这里会进行一些预处理、模型输入等操作\n",
    "    print(f\"Processing {text} with label {label}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.utils.data import Dataset, DataLoader\n",
    "\n",
    "class TextDataset(Dataset):\n",
    "    def __init__(self, csv_file):\n",
    "        # 使用 Pandas 加载 CSV 文件\n",
    "        self.data = pd.read_csv(csv_file)\n",
    "    \n",
    "    def __len__(self):\n",
    "        # 返回数据集的总长度\n",
    "        return len(self.data)\n",
    "    \n",
    "    def __getitem__(self, idx):\n",
    "        # 根据索引返回对应的文本和标签\n",
    "        text = self.data.iloc[idx, 1]  # 第二列是文本\n",
    "        label = self.data.iloc[idx, 0]  # 第一列是标签\n",
    "        return text, label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实例化数据集\n",
    "dataset = TextDataset(\"data.csv\")\n",
    "\n",
    "# 创建 DataLoader，指定批次大小、是否打乱数据、以及并行加载的线程数\n",
    "dataloader = DataLoader(dataset, batch_size=4, shuffle=True, num_workers=2)\n",
    "\n",
    "# 模型训练循环\n",
    "for batch in dataloader:\n",
    "    texts, labels = batch\n",
    "    # 这里会进行预处理、模型输入等操作\n",
    "    print(f\"Processing batch with texts: {texts} and labels: {labels}\")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
